<?php
namespace app\index\model;
require_once EXTEND_PATH.'weixinpay/pay/jsapi.php';
use Wxjspay;
class Wx extends BaseModel
{
    public    $configs = [];
    public function __construct(){
        $wxconfig = D('wxconfig');
        $res = $wxconfig->find();
        $this->configs['APPID'] = $res['appid'];
        $this->configs['MCHID'] = $res['mchid'];
        $this->configs['KEY'] = $res['keys'];
        $this->configs['APPSECRET'] = $res['appsecret'];
    }
    
    public function getConfig(){
        return $this->configs;
    }
    
    function wxSdk($url){
        
        $jssdk = new \jssdks\JSSDK($this->configs['APPID'],$this->configs['APPSECRET']);
        $signPackage = $jssdk->GetSignPackage($url);
        return $signPackage;
    }  
    public function getopenId($redirect_uri,$jumpMy=""){
        $url = config('httpurl');
        $openid = session($url);
        if($openid){
            $user = new User();
            $res = $user->getUserWxToken2($openid);
            if($res){
                $volUrl = "https://api.weixin.qq.com/sns/auth?access_token=".$res['access_token']."&openid=$openid";
                $state = request_get($volUrl);
                if(isset($state['errmsg'])&&strtolower($state['errmsg'])=="ok"){
                    //未过期，跳转至目标页面
                    $datas['url'] = $jumpMy;
                    return view('jump',$datas);
                }else {
                    //重新获取授权
                    return $this->openauths($redirect_uri);
                }
            }else{
                return $this->openauths($redirect_uri);
            }
            
        }else {
            return $this->openauths($redirect_uri);
        }
        
        
        
    }
    
    public function getToken($appid,$secret,$code){
        return request_get('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.
            '&secret='.$secret.'&code='.$code.'&grant_type=authorization_code');
    }
    
    public function getUserInfo($access_token,$openid){
       return request_get('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');

    }
    
    public function openauths($redirect_uri){
        $jssdk = new \jssdks\JSSDK($this->configs['APPID'],$this->configs['APPSECRET']);
        $appId = $jssdk->getAppId();
        $state = '0';
        $datas['url'] = 'http://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appId.
        '&redirect_uri='.urlencode($redirect_uri).
        '&response_type=code&scope=snsapi_userinfo&state='.$state.'#wechat_redirect';
        return view('jump',$datas);
    }
  
    public function pay($userid,$openId,$money){
        $data['user_id'] = $userid;
        $data['money'] = $money*100;
        $data['order_id']=build_order_no();
        $wxjspay = new Wxjspay($this->configs);
        $wxjspay->setUrls(config("httpurl"));
        $res = $wxjspay->buildervipPay($openId, $data);
        $pay = new Payhistory();
        $pay->addHistory($data);
        return json_decode($res,true);
        
    }

    function queryOrder($orderId){
        $wxjspay = new Wxjspay($this->configs);
        return $wxjspay->queryOrder($orderId);
    }
    
}